package com.qf.Web.Servlet;/*
 *
 */

import com.qf.Domain.Cart;
import com.qf.Domain.Goods;
import com.qf.Domain.User;
import com.qf.Service.CartsService;
import com.qf.Service.GoodService;
import com.qf.Service.Impl.CartServiceImpl;
import com.qf.Service.Impl.GoodsServiceImpl;
import com.qf.Utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet{
    //添加购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        //判断用户是否登录
        User  user = (User) request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String num = request.getParameter("number");
        int number=1;
        if (StringUtils.isEmtry(goodsId)){
            request.setAttribute("msg", "添加购物车失败");
            return "/message.jsp";
        }
        if (!StringUtils.isEmtry(num)){
            number=Integer.parseInt(num);
            if (number<1){
                number=1;
            }
        }
        //查询商品
        GoodService goodsService=new GoodsServiceImpl();
        try {
            Goods goods = goodsService.fingById(Integer.parseInt(goodsId));
            if (goods!=null) {
                CartsService cartsService = new CartServiceImpl();
                Cart cart=cartsService.findByUidAndPid(user.getId(),goods.getId());
                if (cart==null) {
                    //添加
                    cart = new Cart(user.getId(), goods.getId(), number, goods.getPrice().multiply(new BigDecimal(number)));
                    cartsService.add(cart);
                }else {
                    //更新
                    cart.setNum(cart.getNum()+number);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartsService.modify(cart);
                }
            }
            return "/cartSuccess.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加购物车失败");
            return "/message";
        }
    }
    //查看购物车
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        //判断用户是否登录
        User  user = (User) request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        //创建对象
        CartsService cartService=new CartServiceImpl();
        List<Cart> carts=cartService.fingById(user.getId());
        request.setAttribute("cart", carts);
        return "/cart.jsp";
    }
    //操作购物车
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response){
        //判断用户是否登录
        User  user = (User) request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if (StringUtils.isEmtry(goodsId)){
            return null;
        }
        if (StringUtils.isEmtry(number)){
            return null;
        }
        CartsService cartsService=new CartServiceImpl();
        Cart cart = cartsService.findByUidAndPid(user.getId(),Integer.parseInt(goodsId));
        //进行更新操作
        int num = Integer.parseInt(number);
        if (num!=0){
            cart.setNum(cart.getNum()+num);
            if (cart.getNum()<1){
                //判断更新之后，个数等于0删除
                cartsService.delete(user.getId(),Integer.parseInt(goodsId));
            }else {
                //更新
                GoodService goodService=new GoodsServiceImpl();
                Goods goods = goodService.fingById(Integer.parseInt(goodsId));
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(num)));
                cartsService.modify(cart);
            }
        }else {
            //删除
            cartsService.delete(user.getId(),Integer.parseInt(goodsId));
        }
        return null;
    }
    //清空购物车
    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response){
        //判断用户是否登录
        User  user = (User) request.getSession().getAttribute("user");
        if (user==null){
            return "redirect:/login.jsp";
        }
        CartsService cartsService=new CartServiceImpl();
        cartsService.delete(user.getId());
        return null;
    }
}
